package cloud.daodao.storm.user.controller;

import cloud.daodao.storm.common.model.No;
import cloud.daodao.storm.common.model.Ok;
import cloud.daodao.storm.common.model.Request;
import cloud.daodao.storm.common.model.Response;
import cloud.daodao.storm.common.user.api.RbacApi;
import cloud.daodao.storm.common.user.model.permission.PermissionData;
import cloud.daodao.storm.common.user.model.rbac.RolePermissionData;
import cloud.daodao.storm.common.user.model.rbac.RolePermissionUpsert;
import cloud.daodao.storm.common.user.model.rbac.UserRoleUpsert;
import cloud.daodao.storm.common.user.model.role.RoleData;
import cloud.daodao.storm.common.user.model.role.RoleSelect;
import cloud.daodao.storm.common.user.model.user.UserSelect;
import cloud.daodao.storm.user.service.RbacService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author DaoDao
 */
@RestController
public class RbacController implements RbacApi {

    @Resource
    private RbacService rbacService;

    @Override
    public Response<RolePermissionData> rolePermission(String authorization, Request<No> request) {
        RolePermissionData data = rbacService.rolePermission(authorization);
        return new Response<>(data);
    }

    @Override
    public Response<List<RoleData>> userRoleList(Request<UserSelect> request) {
        List<RoleData> data = rbacService.userRoleList(request.getParam());
        return new Response<>(data);
    }

    @Override
    public Response<List<PermissionData>> rolePermissionList(Request<RoleSelect> request) {
        List<PermissionData> data = rbacService.rolePermissionList(request.getParam());
        return new Response<>(data);
    }

    @Override
    public Response<Ok> userRoleUpsert(Request<UserRoleUpsert> request) {
        rbacService.userRoleUpsert(request.getParam());
        return Response.ok();
    }

    @Override
    public Response<Ok> rolePermissionUpsert(Request<RolePermissionUpsert> request) {
        rbacService.rolePermissionUpsert(request.getParam());
        return Response.ok();
    }

}
